package charlie.shop.web;

import charlie.shop.misc.Common;
import com.charlie.beans.Users;
import com.charlie.db.DAOLocal;
import com.charlie.sessions.UsersFacadeLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Оленка
 */
@WebServlet(name = "Activate", urlPatterns = {"/activate"})
public class Activate extends HttpServlet {

    @EJB
    DAOLocal dao;
    @EJB
    UsersFacadeLocal usersFacade;

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String hash = request.getParameter("hash");
        String email = request.getParameter("email");
        if (dao.getHash(email).equals(hash)) {
            Users user = usersFacade.findByEmail(email);
            switch (user.getUserStatus()) {
                case 0:
                    user.setUserStatus(1);
                    usersFacade.edit(user);
                    request.setAttribute("massage", "User " + email + " was successfully activate");
                    break;
                case 1:
                    request.setAttribute("massage", "User " + email + " is already activated");
                    break;
                case 2:
                    request.setAttribute("massage", "User " + email + " is locked");
                    break;
            }
        } else {
            request.setAttribute("massage", "Can't activate user " + email);
        }
        response.setContentType("text/html;charset=UTF-8");
        getServletContext().getRequestDispatcher("/activate.jsp").forward(request, response);
    }
}
